<template>
	<view class="tui-container">
		<view class="tui-head">
			<view class="tui-head-zuo"></view>
			<view class="tui-head-zhong">我的任务</view>
		</view>
		
		<template v-for="(item,index) in shijuan" :key="index">
			<view class="qukuai_ziti">考试任务：</view>
			<tui-list-cell>
				<view class="tui-news-flex">
					<view class="tui-news-picbox">
						<!-- <image   mode="widthFix" class="tui-block" :src="item.paperimg"></image> -->
						<image   mode="widthFix" class="tui-block" src="/static/images/2.jpg"></image>
					</view>
					<view class="tui-news-tbox tui-flex-column tui-flex-between">
							<view class="tui-news-title">试卷名称：{{item.name}}</view>
							<view class="tui-news-fenshu">试卷总分：{{item.mark}}分</view>
							<view class="tui-btn-box">
								<tui-button width="200rpx" height="50rpx" shape="circle" type="green" @click="startkaoshi(item)">开始考试</tui-button>
							</view>
					</view>
					
				</view>
			</tui-list-cell>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shijuan: []
			}
		},
		onLoad() {
			let _this = this;
			try {
				const empinfo = uni.getStorageSync('empinfo');
				if (empinfo){
					//获取试卷列表接口
					 _this.tui.request('/wx/test/all-test', 'POST', {
						companyId: empinfo.companyId,
					}, false, false,true).then((res) => {
						if (res.code === 200) {
							
							_this.shijuan = res.tests;
							
						}else{
							this.tui.toast(res.msg)
						}
					}).catch((e) => {})
				}else{
					//跳转到登录页面
					uni.reLaunch({
						url: '/pages/index/index'
					});
				}
			}catch (e) {
				console.log('获取token异常');
			}
			
		},
		methods: {
			
			//开始考试
			startkaoshi: function(item) {
				let  _this = this;
				//console.log('click~'+e);
				//this.tui.toast('click~'+item.mark);
				try {
					const empinfo = uni.getStorageSync('empinfo');
					if (empinfo){
						//试卷主表ID
						var testid = item.id;
						//视频任务主表ID
						var learn_video_task_id = item.videoTaskId;
						//允许考试次数
						var examtimes = item.examTimes;
						//允许考试开始时间
						var startdate = item.startDate;
						//允许考试结束日期
						var enddate = item.endDate;
						//时间格式转换
						var startDate = new Date(startdate);
						console.log('startDate===='+startDate);
						      
						var endDate = new Date(enddate);
						console.log('endDate==='+endDate);
						
						//当前时间
						var date = new Date();
						//管理员权限
						var video_admin = empinfo.adminMobile;
						//console.log('video_admin==='+video_admin);
						//拥有管理员权限
						if(video_admin != null){
							
						}else{
							//普通用户
							if((date - startDate) < 0){
								//校验考试开始时间
								this.tui.toast("考试开始时间为:"+startdate+",请在考试开始时间后进行考试！")
							}else if((date - endDate) > 0){
								//校验考试结束时间
								this.tui.toast("考试结束时间为:"+enddate+",已经结束考试，无法再进行考试！")
							}else{
								//校验考试次数
								//考试试卷没有关联视频任务
								if(learn_video_task_id == '' || learn_video_task_id == null){
									//查询对应考试成绩单汇总表数据  获取考试次数，验证是否超出
									 _this.tui.request('/wx/test/get-transcript-sum', 'POST', {
										employeeId: empinfo.employeeId,
										testpaper_id: testid
									}, false, false,true).then((res) => {
										if (res.code === 200) {
											//本次考试次数
											var nowExamTimes = res.transcriptSum.examTimes + 1;
											//超出允许考试次数
											if (res.transcriptSum.examTimes >= examtimes){
												this.tui.toast("您已超出此考试的最大次数，无法再次考试！")
											}else{
												console.log('可以考试');
											}
										}else if(res.code === 201){
											console.log('首次考试');
										}else{
											this.tui.toast(res.msg)
										}
									}).catch((e) => {}) 
								}else{
									
								}
							}
						}
						
					}else{
						//跳转到登录页面
						uni.reLaunch({
							url: '/pages/index/index'
						});
					}
				}catch (e) {
					console.log('获取token异常');
				}
			}
		}
	}
</script>

<style>
.tui-container {
	display: flex;
	flex-direction: column;
	box-sizing: border-box;
	padding-bottom: env(safe-area-inset-bottom);
}
.tui-head{
	display: flex;
	align-items:center;
	background-color: #f9f9f9;
}
.tui-head-zuo{
	margin: 5px 10px; 
	width:5px; 
	height:20px; 
	background-color: rgb(13, 163, 233);
	border-radius: 10px;
}
.tui-head-zhong{
	font-size: 16px; 
	flex-grow: 1;
	color: rgb(13, 163, 233);
}
.qukuai_ziti {
	font-size: 16px; 
	flex-grow: 1;
	color: rgb(132, 177, 7);
	align-items: center;
}
.shijuan{padding: 5px; 
  display: flex;
  box-sizing: border-box;
  border-bottom: 1px dotted gainsboro;
}
.tui-news-flex {
	width: 100%;
	display: flex;
	align-items: flex-start !important;
}
.tui-news-picbox {
	display: flex;
	position: relative;
	width: 220rpx;
	height: 220rpx;
}
.tui-block {
	display: block;
}
.tui-news-tbox {
	flex: 1;
	width: 100%;
	box-sizing: border-box;
	display: flex;
}
.tui-flex-column {
	flex-direction: column !important;
}
.tui-flex-between {
	justify-content: space-between !important;
	/* height: 140rpx; */
	padding-left: 20rpx;
}
.tui-news-title {
	width: 100%;
	font-size: 28rpx;
	word-break: break-all;
	word-wrap: break-word;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	box-sizing: border-box;
	padding-top: 10rpx;
}
.tui-news-fenshu {
	width: 100%;
	font-size: 24rpx;
	word-break: break-all;
	word-wrap: break-word;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	box-sizing: border-box;
	padding-top: 5rpx;
}
.tui-btn-box {
	padding-top: 20rpx;
	padding-left: 80rpx;
}


</style>
